package com.chinasoft.mall.framework;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeSet;

import android.util.Log;

import com.chinasoft.mall.model.response.EpgBean;
import com.chinasoft.mall.util.DateUtil;

/**
 * 当天节目单列表
 * 
 * @author Administrator
 * 
 */
public class LivingGoods extends Observable {
	private static LivingGoods _lg;
	private Object obj = new Object();
	private Object ts = new Object();
	private int ts_type = 0;
	// 提示类型
	public static final int Refresh_TS = 0;
	public static final int Refresh_PLAY = 1;
	// 提示直播信息
	private EpgBean eb;

	private boolean isRefresh = true;

	private List<EpgBean> ts_ebs = new ArrayList<EpgBean>();
	// 当天总节目单
	private List<EpgBean> cur_ebs;
	// 等待直播
	private List<EpgBean> wait_ebs = new ArrayList<EpgBean>();
	// 正在直播
	private LinkedList<EpgBean> living_ebs = new LinkedList<EpgBean>();
	// 直播完毕
	private List<EpgBean> lived_ebs = new ArrayList<EpgBean>();
	private Timer timer = new Timer();
	private TimerTask tt ;
	public void setRefresh(boolean isRefresh) {
		this.isRefresh = isRefresh;
	}

	/**
	 * 刷新提示列表
	 * 
	 * @return
	 */
	public void invalidate() {
		isRefresh = false;
		synchronized (ts) {
			ts_ebs.clear();
			if (cur_ebs != null)
				for (EpgBean e : cur_ebs) {
					if (e.isNotify()) {
						ts_ebs.add(e);
					}
				}
		}
		isRefresh = true;
	}

	protected void reTs() {
		if (!isRefresh) {
			return;
		}
		synchronized (ts) {
			for (EpgBean e : ts_ebs) {
				if (!isRefresh) {
					return;
				}
				if (DateUtil.compare3Date(e.getFORM_FR_DATE())) {
					eb = e;
					eb.setNotify(false);
					invalidate();
					setChanged();
					notifyObservers(Refresh_TS);
					return;
				}
			}
		}
	}

	public List<EpgBean> getWait_ebs() {
		synchronized (obj) {
			return wait_ebs;
		}
	}

	public LinkedList<EpgBean> getLiving_ebs() {
		synchronized (obj) {
			return living_ebs;
		}
	}

	public List<EpgBean> getLived_ebs() {
		synchronized (obj) {
			return lived_ebs;
		}
	}

	public EpgBean getEb() {
		return eb;
	}

	public void setEb(EpgBean eb) {
		this.eb = eb;
	}

	/**
	 * 更新数据
	 * 
	 * @param cur_ebs
	 */
	public void setEpgBeans(List<EpgBean> cur_ebs) {
		// TreeSet<EpgBean> teb = new TreeSet<EpgBean>();
		// teb.addAll(cur_ebs);
		// this.cur_ebs = new ArrayList<EpgBean>(teb);
		this.cur_ebs = cur_ebs;
		reFreshEpgBeans();
	}

	public List<EpgBean> getEpgBeans() {
		return cur_ebs;
	}

	/**
	 * 刷新节目单数据
	 * 
	 * @param cur_ebs
	 */
	private synchronized void reFreshEpgBeans() {
		if (cur_ebs != null) {
			synchronized (obj) {
				wait_ebs.clear();
				living_ebs.clear();
				lived_ebs.clear();
			}
			for (EpgBean eb : cur_ebs) {
				byte ebState = DateUtil.CompareTime(eb.getFORM_FR_DATE(),
						eb.getFORM_END_DATE());
				eb.setLivingState(ebState);
				synchronized (obj) {
					switch (ebState) {
					// 等待直播
					case -1:
						wait_ebs.add(eb);
						break;
					// 正在直播
					case 0:
						living_ebs.add(eb);
						break;
					// 直播完毕
					case 1:
						lived_ebs.add(eb);
						break;
					default:
						break;
					}
				}
			}
			setChanged();
			notifyObservers(Refresh_PLAY);
		}
	}

	private LivingGoods() {
	};

	public void start() {
		if(tt!=null)
		{
			tt.cancel();
		}
		tt= new TimerTask() {
			@Override
			public void run() {
				if (++ts_type == 60) {
					LivingGoods.this.reFreshEpgBeans();
					ts_type = 0;
				}
				LivingGoods.this.reTs();
			}
		};
		timer.schedule(tt, 5000, 1000);
	}

	public static LivingGoods getInstance() {
		synchronized (LivingGoods.class) {
			if (_lg == null) {
				_lg = new LivingGoods();
			}
		}
		return _lg;
	}

	public void cancle() {
		if (timer != null)
			timer.cancel();
		_lg = null;
	}

	public EpgBean getLivingEpg() {
		if (cur_ebs != null) {
			for (EpgBean eb : cur_ebs) {
				if (eb.getLivingState() == 0) {
					return eb;
				}
			}
		}
		return null;
	}

}
